package com.hgd.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.hgd.client.admin.Admin;
import com.hgd.client.admin.AdminImplDelegate;
import com.hgd.client.admin.AdminImplService;
import com.hgd.client.admin.AdminLogin;
import com.hgd.client.admin.ManagementStation;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{
	private int flag = 0;
	private int isEnable = 0;
	private AdminLogin adminLogin;
	private Admin admin = new Admin();
	AdminImplService service = new AdminImplService();			
	AdminImplDelegate delegate = service.getAdminImplPort();
	
	public int getIsEnable() {
		return isEnable;
	}
	public void setIsEnable(int isEnable) {
		this.isEnable = isEnable;
	}
	public int getFlag() {
		return flag;
	}
	public void setFlag(int flag) {
		this.flag = flag;
	}
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	
	public AdminLogin getAdminLogin() {
		return adminLogin;
	}
	public void setAdminLogin(AdminLogin adminLogin) {
		this.adminLogin = adminLogin;
	}
	
	public String superLogin() throws Exception{
		List<Admin> adminList = new ArrayList<Admin>();
		String loginResult;
		adminList = delegate.superLogin(adminLogin.getArg0(), adminLogin.getArg1());
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (adminList.size()>0) {
			admin = adminList.get(0);
			isEnable = admin.getIsEnabled();
			System.out.println(admin.getRoles()+admin.getAdminName());
			if (isEnable==1) {
				if (admin.getRoles()==1) {
					session.put("superUser", admin.getAdminName());		
					session.put("userId", admin.getId());
					loginResult = "superAdmin";
				}else{
					session.put("commonUser", admin.getAdminName());		
					session.put("userId", admin.getId());
					loginResult = "commonAdmin";
				}
			}else{
				isEnable = 2;
				loginResult = "disabled";
			}
		} else {
			flag = 1;
			loginResult = ERROR;
		}
		System.out.println(loginResult);
		return loginResult;
	}

	public String commonLogin() throws Exception {
		List<Admin> adminList = new ArrayList<Admin>();
		String loginResult;
		adminList = delegate.commonLogin(adminLogin.getArg0(), adminLogin.getArg1());
		Map<String, Object> session = ActionContext.getContext().getSession();
		if (adminList.size()>0) {
			admin = adminList.get(0);
			isEnable = admin.getIsEnabled();
			if (isEnable==1) {
				if (admin.getRoles()==1) {
					session.put("superUser", admin.getAdminName());		
					session.put("userId", admin.getId());
					loginResult = "superAdmin";
				}else{
					session.put("commonUser", admin.getAdminName());						
					session.put("userId", admin.getId());

						if (admin.getManagementStation()!=null) {
							int MSId = 0;
							int Init = 0;
							String msName;
							MSId = admin.getManagementStation().getMSId();
							Init = admin.getManagementStation().getIsInitialize();
							msName = admin.getManagementStation().getMSName();
							session.put("MSId",MSId);
							session.put("Init",Init);
							session.put("msName",msName);
							
						}
						loginResult = "commonAdmin";
				}
					
				}
			else{
				isEnable = 2;
				loginResult = "disabled";
			}
		} else {
			flag = 1;
			loginResult = ERROR;
		}
		return loginResult;
	}

}
